package com.roc.template.redis;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisPoolConfig {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Value("${spring.redis.jedis.pool.max-active}")
	private int maxActive;
	@Value("${spring.redis.sentinel.master}")
	private String master;
	@Value("${spring.redis.sentinel.nodes}")
	private String nodes;
	@Value("${spring.redis.sentinel.password}")
	private String password;
	@Value("${spring.redis.maxIdle}")
	private int maxIdle;
	@Value("${spring.redis.maxTotal}")
	private int maxTotal;
	@Value("${spring.redis.maxWaitMillis}")
	private int maxWaitMillis;
	@Value("${spring.redis.testOnBorrow}")
	private boolean testOnBorrow;

	@Bean(name = "stringRedisTemplate")
	public StringRedisTemplate stringRedisTemplate() {
		StringRedisTemplate template = new StringRedisTemplate();
		JedisConnectionFactory j = connectionFactory(maxIdle, maxTotal, maxWaitMillis, testOnBorrow);
		template.setConnectionFactory(j);
		return template;
	}

	@Bean(name = "redisTemplate")
	public RedisTemplate<Object,Object> redisTemplate() {
		RedisTemplate<Object,Object> template = new RedisTemplate<>();
		JedisConnectionFactory j = connectionFactory(maxIdle, maxTotal, maxWaitMillis, testOnBorrow);
		template.setConnectionFactory(j);
		return template;
	}

	public JedisConnectionFactory connectionFactory(int maxIdle, int maxTotal, long maxWaitMillis,
                                                    boolean testOnBorrow) {
		JedisConnectionFactory jedis = new JedisConnectionFactory(sentinelConfig(master, nodes),
				poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
		// 初始化连接pool
		jedis.afterPropertiesSet();
		return jedis;
	}

	private RedisSentinelConfiguration sentinelConfig(String master, String nodes) {
		RedisSentinelConfiguration sentinel = new RedisSentinelConfiguration();
		sentinel.setMaster(master);

		if (StringUtils.isNotBlank(password)) { sentinel.setPassword(password); }
		logger.info("初始化密码信息"+password);

		String[] host = nodes.split(",");
		for (String redisHost : host) {
			String[] item = redisHost.split(":");
			String ip = item[0];
			String port = item[1];
			sentinel.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
		}
		return sentinel;
	}

	public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
		JedisPoolConfig poolCofig = new JedisPoolConfig();
		poolCofig.setMaxIdle(maxIdle);
		poolCofig.setMaxTotal(maxTotal);
		poolCofig.setMaxWaitMillis(maxWaitMillis);
		poolCofig.setTestOnBorrow(testOnBorrow);
		return poolCofig;
	}
}
